package com.example.demo.controller;

import com.example.demo.dataobject.BloodSugarRecord;
import com.example.demo.form.BloodSugarRecordForm;
import com.example.demo.service.BloodSugarRecordService;
import com.example.demo.service.BloodSugarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/bloodSugar")
@Slf4j
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")  //解决跨域请求,但是无法做到对于session的共享，无法针对ajax请求，所以需要对参数值进行设置
public class BloodSugarController {


    @Autowired
    private BloodSugarRecordService bloodSugarRecordService;


//    @RequestMapping("/saveBloodSugarRecord")
//    public String saveBloodSugarRecord(@RequestParam("bloodSugar") Double bloodSugar,
//                                       @RequestParam("telephone") String telephone,
//                                       @RequestParam("periodType") Integer periodType,
//                                       @RequestParam("date") Date date,
//                                       @RequestParam("recordTime") Timestamp recordTime,
//                                       @RequestParam("remarks") String remarks) {
//        BloodSugarRecord result = bloodSugarRecordService.save(bloodSugar, telephone, periodType, date, recordTime, remarks);
//        if (result == null) {
//            return "ERROR";
//        }
//        return "SUCCESS";
//    }


    @RequestMapping("/saveBloodSugarRecord")
    public String saveBloodSugarRecord(@Valid BloodSugarRecordForm bloodSugarRecordForm,
                                       BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            log.error("血糖数据中存在错误，bloodSugarRecordForm={}",bloodSugarRecordForm);
        }
        return "SUCCESS";
    }


    @RequestMapping("/listBloodSugarRecord")
    public List<BloodSugarRecord> listBloodSugarRecord(@RequestParam("telephone") String telephone) {
        List<BloodSugarRecord> result = bloodSugarRecordService.findByTelephone(telephone);
        return result;
    }


    /**
     * 请求路径和方法名改为searchSugarRecordInADay
     *
     * @param telephone
     * @param date
     * @return
     */
    @RequestMapping("/searchBloodSugarRecordInADay")
    public List<BloodSugarRecord> searchBloodSugarRecordInADay(@RequestParam("telephone") String telephone,
                                                               @RequestParam("date") Date date) {
        List<BloodSugarRecord> result = bloodSugarRecordService.findByTelephoneAndDateOrderByRecordTime(telephone, date);
        return result;
    }

    @RequestMapping("/deleteBloodSugarRecord")
    public String deleteBloodSugarRecord(@RequestParam("id") Integer id) {
        Boolean result = bloodSugarRecordService.delete(id);
        if (result) {
            return "SUCCESS";
        }
        return "ERROR";
    }


    @RequestMapping("/modifyBloodSugarRecord")
    public String modifyBloodSugarRecord(@RequestParam("id") Integer id,
                                         @RequestParam("bloodSugar") Double bloodSugar,
                                         @RequestParam("telephone") String telephone,
                                         @RequestParam("periodType") Integer periodType,
                                         @RequestParam("date") Date date,
                                         @RequestParam("recordTime") Timestamp recordTime,
                                         @RequestParam("remarks") String remarks) {
        BloodSugarRecord result = bloodSugarRecordService.saveModification(id, bloodSugar, telephone, periodType, date, recordTime, remarks);
        if (result == null) {
            return "ERROR";
        }
        return "SUCCESS";
    }


    @RequestMapping("/findLastSugarRecord")
    public BloodSugarRecord findLastSugarRecord(@RequestParam("telephone") String telephone) {
        BloodSugarRecord result = bloodSugarRecordService.findLastSugarRecord(telephone);
        return result;
    }
    @RequestMapping("/findBloodSugarRecordInAWeek")
    public List<BloodSugarRecord> findBloodSugarRecordInAWeek(@RequestParam("telephone") String telephone,
                                                              @RequestParam("date") Date date) {
        List<BloodSugarRecord> result = bloodSugarRecordService.findByUserIdAndDateInAWeek(telephone, date);
        return result;
    }

    @RequestMapping("/findBloodSugarRecordInAPeriod")
    public List<BloodSugarRecord> findBloodSugarRecordInAPeriod(@RequestParam("telephone") String telephone,
                                                                @RequestParam("date") Date date) {
        List<BloodSugarRecord> result = bloodSugarRecordService.findByUserIdAndDateInAPeriod(telephone, date);
        return result;
    }




}
